/*
 * Copyright (c) 2009 XMock contributors
 * This program is made available under the terms of the Apache License.
 */
package org.xmock.controller;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.thoughtworks.xstream.XStream;

public class LocalMockCaseManager implements MockCaseManager
{
	private static final Logger logger = Logger.getLogger(MockProperties.class);
	public static LocalMockCaseManager instance;
	private static String defaultCaseLocation = ".";
	private static String defaultCaseFileName = "mockcase.xml";
	
	private List<MockCase> mockCases = new ArrayList<MockCase>();
	private File caseFile;
	private String caseLocation;
	private String caseFileName;
	
	public static LocalMockCaseManager getInstance()
	{
		if(instance == null)
		{
			instance = new LocalMockCaseManager();
		}
		return instance;
	}
	
	private LocalMockCaseManager()
	{
		init();
	}
	
	@SuppressWarnings("unchecked")
	private void init()
	{
		MockProperties properties = MockProperties.getInstance();
		caseLocation = properties.getProperty("mock.case.store.location");
		caseFileName = properties.getProperty("mock.case.store.filename");
		if(caseLocation==null || caseFileName==null)
		{
			logger.error("Could not find mock cases file");
			caseLocation=defaultCaseLocation;
			caseFileName=defaultCaseFileName;
		}
		caseFile = new File(caseLocation + File.separator + caseFileName);
		if(caseFile.exists())
		{
			XStream xstream = new XStream();
			try 
			{
				FileReader fr = new FileReader(caseFile);
				Object casesObj = xstream.fromXML(fr);
				fr.close();
				if(casesObj instanceof List)
				{
					mockCases = (List<MockCase>) casesObj ;
				}
				else
				{
					logger.error("Error reading mock case from file");
				}
			} 
			catch (Exception e) 
			{
				logger.error("Error reading mock case from file", e);
			}
		}
	}
	
	public void addMockCase(String caseName, String caseId) {
		if(caseFile==null)
		{
			logger.error("Could not find test cases file");		
			return;
		}
		MockCase mockCase = new MockCase(caseName, caseId);
		addMockCase(mockCase);
	}

	public void addMockCase(MockCase mockCase) {
		if(caseFile==null)
		{
			logger.error("Could not find mock cases file");	
			return;
		}
		deleteMockCase(mockCase.getName(), mockCase.getId());
		mockCases.add(mockCase);
		XStream xstream = new XStream();
		try 
		{
			FileWriter fw = new FileWriter(caseFile);
			xstream.toXML(mockCases, fw);
			fw.close();
		} 
		catch (Exception e) 
		{
			logger.error("Error writing mock case to file", e);
		}
	}

	public MockCase getMockCase(String caseName, String caseId) {
		if(caseFile==null || !caseFile.exists())
		{
			logger.error("Could not find mock cases file");	
			return null;
		}
		MockCase mockCase = null;
		for(int i=0; i<mockCases.size(); i++)
		{
			MockCase mockCasetmp = mockCases.get(i);
			if(mockCasetmp.getName().equals(caseName) && mockCasetmp.getId().equals(caseId))
			{
				mockCase = mockCasetmp;
				break;
			}
		}
		return mockCase;
	}

	public boolean isMockCase(String caseName, String caseId) {
		if(caseFile==null)
		{
			logger.error("Could not find test cases file");		
			return false;
		}
		return getMockCase(caseName, caseId)!=null;
	}
	
	public boolean deleteMockCase(String caseName, String caseId)
	{
		boolean caseExists = false;
		for(int i=0; i<mockCases.size(); i++)
		{
			MockCase mockCase = mockCases.get(i);
			if(mockCase.getName().equals(caseName) && mockCase.getId().equals(caseId))
			{
				mockCases.remove(i);
				caseExists = true;
				break;
			}
		}
		return caseExists;
	}

}
